package com.oa.pm.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.joneying.common.annotation.CommonService;
import com.oa.core.PageResult;
import com.oa.core.utils.DateUtil;
import com.oa.core.utils.ExcelUtil;
import com.oa.pm.dto.ResumeAddDTO;
import com.oa.pm.dto.ResumeListParamDTO;
import com.oa.pm.entity.Resume;
import com.oa.pm.entity.ResumeFile;
import com.oa.pm.mapper.ResumeMapper;
import com.oa.pm.service.IResumeFileService;
import com.oa.pm.service.IResumeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oa.pm.vo.ResumeDetailsVO;
import com.oa.pm.vo.ResumeListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 简历库表 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class ResumeServiceImpl extends ServiceImpl<ResumeMapper, Resume> implements IResumeService {

    private final ResumeMapper resumeMapper;

    @Autowired
    private IResumeFileService resumeFileService;

    @Autowired
    public ResumeServiceImpl(ResumeMapper resumeMapper) {
        this.resumeMapper = resumeMapper;
    }

    @Override
    @CommonService
    public PageResult<Resume> findList(Integer pageNum, Integer pageSize) {
        if(pageNum == null){
            List<Resume> list = resumeMapper.findList();
            return new PageResult<>(list);
        }else{
            // 当前页，总条数 构造 page 对象
            Page<Resume> page = new Page<>(pageNum, pageSize);
            //查询
            List<Resume> list = resumeMapper.findList(page);
            return new PageResult<>(list);
        }
    }

    /**
    * 根据id查询
    */
    @Override
    @CommonService
    public ResumeDetailsVO findResumeById(String id){
        return resumeMapper.findResumeDetails(id);
    }

    @Override
    public PageResult<ResumeListVO> findResumeList(ResumeListParamDTO resumeListParamDTO) {
        return new PageResult(resumeMapper.findResumeList(new Page(resumeListParamDTO.getPageNum(), resumeListParamDTO.getPageSize()), resumeListParamDTO));
    }

    @Transactional
    @Override
    public boolean addResume(ResumeAddDTO resume) {
        if(resumeMapper.insert(resume.getResume()) > 0){
            ResumeFile resumeFile = resume.getResumeFile();
            resumeFile.setResumeId(resume.getResume().getId());
            resumeFileService.insert(resumeFile);
            return true;
        }
        return false;
    }

    @Override
    public boolean editResume(ResumeAddDTO resume) {
        if(resumeMapper.updateById(resume.getResume()) > 0){

            ResumeFile resumeFile = resume.getResumeFile();
            if(resumeFile != null){
                resumeFileService.delete(new EntityWrapper().eq("resume_id", resume.getResume().getId()));
                resumeFile.setResumeId(resume.getResume().getId());
                resumeFileService.insert(resumeFile);
                return true;
            }
        }
        return false;
    }

    @Override
    public void export(ResumeListParamDTO resumeListParamDTO, HttpServletResponse response) {
        List<ResumeListVO> resumeListVOS;
        if(resumeListParamDTO.getPageNum() != null){
            Page page = new Page(resumeListParamDTO.getPageNum(), resumeListParamDTO.getPageSize());
            resumeListVOS = resumeMapper.findResumeList(page, resumeListParamDTO);
        }else{
            resumeListVOS = resumeMapper.findResumeList(new Page(), resumeListParamDTO);
        }
        String[] rows = {"id", "投递岗位", "姓名", "年龄", "性别", "文件名称", "文件地址"};
        try {
            ExcelUtil.downloadExcel("简历库列表" + DateUtil.getDayMonth(), rows, resumeListVOS, "简历库列表" + DateUtil.getDayMonth()+".xls", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
